<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="需求项目中需要用到倒计时View，因为需求需要，所以不能只依靠本地计时。还需要网络请求数据之后，矫正时间。 所以本文的目标如下：  自定义字体 依赖网络的倒计时 时间矫正 倒计时的自动更新  最终效果如下：  分析通过自定义控件去实现，然后通过HashMap存储对应奖期以及倒计时信息，然后通过定时的网络请求，去矫正计时。 因此本文设计控件继承自AppCompatTextView， 增加了设置初始时">
<meta property="og:type" content="article">
<meta property="og:title" content="Android自定义View-倒计时控件（可自动继续，矫正时间）">
<meta property="og:url" content="http://vchao.top/2021/08/25/Android%E8%87%AA%E5%AE%9A%E4%B9%89View-%E5%80%92%E8%AE%A1%E6%97%B6%E6%8E%A7%E4%BB%B6%EF%BC%88%E5%8F%AF%E8%87%AA%E5%8A%A8%E7%BB%A7%E7%BB%AD%EF%BC%8C%E7%9F%AB%E6%AD%A3%E6%97%B6%E9%97%B4%EF%BC%89/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="需求项目中需要用到倒计时View，因为需求需要，所以不能只依靠本地计时。还需要网络请求数据之后，矫正时间。 所以本文的目标如下：  自定义字体 依赖网络的倒计时 时间矫正 倒计时的自动更新  最终效果如下：  分析通过自定义控件去实现，然后通过HashMap存储对应奖期以及倒计时信息，然后通过定时的网络请求，去矫正计时。 因此本文设计控件继承自AppCompatTextView， 增加了设置初始时">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="c:/Users/zly/Desktop/倒计时.png">
<meta property="article:published_time" content="2021-08-25T14:42:47.349Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.794Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="自定义View">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:/Users/zly/Desktop/倒计时.png">

<link rel="canonical" href="http://vchao.top/2021/08/25/Android%E8%87%AA%E5%AE%9A%E4%B9%89View-%E5%80%92%E8%AE%A1%E6%97%B6%E6%8E%A7%E4%BB%B6%EF%BC%88%E5%8F%AF%E8%87%AA%E5%8A%A8%E7%BB%A7%E7%BB%AD%EF%BC%8C%E7%9F%AB%E6%AD%A3%E6%97%B6%E9%97%B4%EF%BC%89/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Android自定义View-倒计时控件（可自动继续，矫正时间） | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/Android%E8%87%AA%E5%AE%9A%E4%B9%89View-%E5%80%92%E8%AE%A1%E6%97%B6%E6%8E%A7%E4%BB%B6%EF%BC%88%E5%8F%AF%E8%87%AA%E5%8A%A8%E7%BB%A7%E7%BB%AD%EF%BC%8C%E7%9F%AB%E6%AD%A3%E6%97%B6%E9%97%B4%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Android自定义View-倒计时控件（可自动继续，矫正时间）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:47 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:47+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h1><p>项目中需要用到倒计时View，因为需求需要，所以不能只依靠本地计时。还需要网络请求数据之后，矫正时间。</p>
<p>所以本文的目标如下：</p>
<ul>
<li>自定义字体</li>
<li>依赖网络的倒计时</li>
<li>时间矫正</li>
<li>倒计时的自动更新</li>
</ul>
<p>最终效果如下：</p>
<p><img src="C:\Users\zly\Desktop\倒计时.png" alt="倒计时"></p>
<h1 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h1><p>通过自定义控件去实现，然后通过HashMap存储对应奖期以及倒计时信息，然后通过定时的网络请求，去矫正计时。</p>
<p>因此本文设计控件继承自AppCompatTextView， 增加了设置初始时间，开始计时，更新计时，停止计时，取消计时，矫正计时，设置字体等若干方法。</p>
<h1 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h1><h2 id="加载自定义字体"><a href="#加载自定义字体" class="headerlink" title="加载自定义字体"></a>加载自定义字体</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 加载自定义字体</span></span><br><span class="line">Typeface TEXT_TYPE;</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    TEXT_TYPE = Typeface.createFromAsset(context.getAssets(), <span class="string">&quot;fonts/Quartz_Regular.ttf&quot;</span>);</span><br><span class="line">&#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">    LogUtils.e(<span class="string">&quot;加载第三方字体失败。&quot;</span>);</span><br><span class="line">    TEXT_TYPE = <span class="keyword">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (TEXT_TYPE != <span class="keyword">null</span>) &#123;</span><br><span class="line">    setTypeface(TEXT_TYPE);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="开始计时"><a href="#开始计时" class="headerlink" title="开始计时"></a>开始计时</h2><p>开始计时，首先通过接口回调，对外暴露方法，方便进行开始前的准备工作。需要传入期次和结束时间两个参数。</p>
<p>更新时间通过handler去实现。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(<span class="keyword">final</span> <span class="keyword">int</span> term_no, <span class="keyword">final</span> <span class="keyword">long</span> end_time)</span> </span>&#123;</span><br><span class="line">    timeListener.timeStart();</span><br><span class="line"></span><br><span class="line">    contine_tag = <span class="keyword">true</span>;</span><br><span class="line">    <span class="keyword">this</span>.end_time = end_time;</span><br><span class="line">    <span class="keyword">this</span>.term_no = term_no;</span><br><span class="line">    <span class="keyword">if</span> (mTimerMap.get(term_no) == <span class="keyword">null</span>) &#123;</span><br><span class="line">        Timer timer = <span class="keyword">new</span> Timer();</span><br><span class="line">        Term_view_bean term_view_bean = <span class="keyword">new</span> Term_view_bean(timer, term_no, end_time);</span><br><span class="line">        mTimerMap.put(term_no, term_view_bean);</span><br><span class="line">        mTimerMap.get(term_no).getTimer().schedule(<span class="keyword">new</span> TimerTask() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                <span class="keyword">if</span> (contine_tag) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (is_over) &#123;</span><br><span class="line">                        <span class="keyword">this</span>.cancel();</span><br><span class="line">                        <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    mHandler.sendEmptyMessage(ticket_mode);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;, <span class="number">0</span>, <span class="number">1000</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="结束"><a href="#结束" class="headerlink" title="结束"></a>结束</h2><p>结束计时，共分为2种，取消指定期次，和取消全部期次。</p>
<blockquote>
<p>取消全部期次计时： 当不需要页面倒计时的时候调用，因为倒计时是采用Timer进行维护的，所以需要手动释放，以避免内存泄露相关事宜。</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stopAll</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Iterator it = mTimerMap.entrySet().iterator();</span><br><span class="line">            <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">                Map.Entry entry = (Map.Entry) it.next();</span><br><span class="line">                <span class="keyword">int</span> key = (<span class="keyword">int</span>) entry.getKey();</span><br><span class="line">                Term_view_bean bean = (Term_view_bean) entry.getValue();</span><br><span class="line">                bean.getTimer().cancel();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        contine_tag = <span class="keyword">false</span>;</span><br><span class="line">        is_over = <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br></pre></td></tr></table></figure>

<blockquote>
<p>取消指定期次计时： 某一个期次奖期结束，根据期次号或者存储的值进行停止</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">   </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stop</span><span class="params">(<span class="keyword">int</span> position_term_no)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mTimerMap.get(position_term_no) != <span class="keyword">null</span>) &#123;</span><br><span class="line">            mTimerMap.get(position_term_no).getTimer().cancel();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stopSaveValue</span><span class="params">(<span class="keyword">int</span> value)</span> </span>&#123;</span><br><span class="line">    Iterator it = mTimerMap.entrySet().iterator();</span><br><span class="line">    <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">        Map.Entry entry = (Map.Entry) it.next();</span><br><span class="line">        <span class="keyword">int</span> key = (<span class="keyword">int</span>) entry.getKey();</span><br><span class="line">        <span class="keyword">if</span> (key != value) &#123;</span><br><span class="line">            Term_view_bean bean = (Term_view_bean) entry.getValue();</span><br><span class="line">            bean.getTimer().cancel();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="时间显示格式化"><a href="#时间显示格式化" class="headerlink" title="时间显示格式化"></a>时间显示格式化</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 时间显示格式化</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> second</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> String <span class="title">second2TimeSecond</span><span class="params">(<span class="keyword">long</span> second)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">long</span> days = second / (<span class="number">3600</span> * <span class="number">24</span>);</span><br><span class="line">    <span class="keyword">long</span> hours = (second / <span class="number">3600</span>) % <span class="number">24</span>;</span><br><span class="line">    <span class="keyword">long</span> minutes = (second % <span class="number">3600</span>) / <span class="number">60</span>;</span><br><span class="line">    <span class="keyword">long</span> seconds = second % <span class="number">60</span>;</span><br><span class="line"></span><br><span class="line">    String showTime = <span class="string">&quot;&quot;</span>;</span><br><span class="line">    <span class="keyword">if</span> (days &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        showTime = days + <span class="string">&quot;天 &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (hours &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (hours &lt; <span class="number">10</span>) &#123;</span><br><span class="line">            showTime += <span class="string">&quot;0&quot;</span> + hours + <span class="string">&quot;时 &quot;</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            showTime += hours + <span class="string">&quot;时 &quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (minutes &lt; <span class="number">10</span>) &#123;</span><br><span class="line">        showTime += <span class="string">&quot;0&quot;</span> + minutes + <span class="string">&quot;:&quot;</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        showTime += <span class="string">&quot;&quot;</span> + minutes + <span class="string">&quot;:&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (seconds &lt; <span class="number">10</span>) &#123;</span><br><span class="line">        showTime += <span class="string">&quot;0&quot;</span> + seconds;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        showTime += <span class="string">&quot;&quot;</span> + seconds;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> showTime;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="修正倒计时"><a href="#修正倒计时" class="headerlink" title="修正倒计时"></a>修正倒计时</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 修正当前倒计时信息</span><br><span class="line"> */</span><br><span class="line">public void updateTimer(final int term_no, final long end_time) &#123;</span><br><span class="line">    this.end_time = end_time;</span><br><span class="line">    this.term_no = term_no;</span><br><span class="line">    if (mTimerMap.get(term_no) == null) &#123;</span><br><span class="line">        start(term_no, end_time);</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        mTimerMap.get(term_no).setEnd_time(end_time);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p>使用的时候，首先初始化，传入handler，然后定时进行网络请求即可呈现效果。</p>
<h1 id="完整代码"><a href="#完整代码" class="headerlink" title="完整代码"></a>完整代码</h1><p>完整代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CountdownTextView</span> <span class="keyword">extends</span> <span class="title">android</span>.<span class="title">support</span>.<span class="title">v7</span>.<span class="title">widget</span>.<span class="title">AppCompatTextView</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> what_count_down_k3 = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    Map&lt;Integer, Term_view_bean&gt; mTimerMap;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> term_no;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> ticket_mode;</span><br><span class="line">    <span class="keyword">private</span> TimeListener timeListener;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">long</span> end_time = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span> contine_tag = <span class="keyword">true</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span> is_over = <span class="keyword">false</span>;</span><br><span class="line">    <span class="keyword">private</span> Handler mHandler = <span class="keyword">new</span> Handler() &#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleMessage</span><span class="params">(android.os.Message msg)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">switch</span> (msg.what) &#123;</span><br><span class="line">                <span class="keyword">case</span> what_count_down_k3:<span class="comment">// 10 分钟一期</span></span><br><span class="line">					<span class="keyword">if</span> (end_time - TimeManager.getInstance().getServiceTime() &lt;= Config.K3_R006_Time_End_sale * <span class="number">1000</span>) &#123;                 </span><br><span class="line">                        timeListener.buyDown();</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">if</span> (end_time - TimeManager.getInstance().getServiceTime() &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">                        timeListener.timedown();</span><br><span class="line">                        <span class="keyword">while</span> (end_time - TimeManager.getInstance().getServiceTime() &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">                            term_no++;</span><br><span class="line">                            end_time += <span class="number">10</span> * <span class="number">60</span> * <span class="number">1000</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                        setTimeText();</span><br><span class="line">                        <span class="keyword">try</span> &#123;</span><br><span class="line">                            stopSaveValue(term_no);</span><br><span class="line">                        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                            e.printStackTrace();</span><br><span class="line">                        &#125;</span><br><span class="line">                        start(term_no, end_time);</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        setTimeText();</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">CountdownTextView</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(context);</span><br><span class="line">        initTextType(context);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">CountdownTextView</span><span class="params">(Context context, AttributeSet attrs)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(context, attrs);</span><br><span class="line">        initTextType(context);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">CountdownTextView</span><span class="params">(Context context, AttributeSet attrs, <span class="keyword">int</span> defStyleAttr)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(context, attrs, defStyleAttr);</span><br><span class="line">        initTextType(context);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">setTimeText</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        setText(second2TimeSecond((end_time - TimeManager.getInstance().getServiceTime()) / <span class="number">1000</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initTextType</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 加载自定义字体</span></span><br><span class="line">        Typeface TEXT_TYPE;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            TEXT_TYPE = Typeface.createFromAsset(context.getAssets(), <span class="string">&quot;fonts/Quartz_Regular.ttf&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            LogUtils.e(<span class="string">&quot;加载第三方字体失败。&quot;</span>);</span><br><span class="line">            TEXT_TYPE = <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (TEXT_TYPE != <span class="keyword">null</span>) &#123;</span><br><span class="line">            setTypeface(TEXT_TYPE);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> ticket_mode, TimeListener timeListener)</span> </span>&#123;</span><br><span class="line">        mTimerMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        <span class="keyword">this</span>.timeListener = timeListener;</span><br><span class="line">        <span class="keyword">this</span>.ticket_mode = ticket_mode;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(<span class="keyword">final</span> <span class="keyword">int</span> term_no, <span class="keyword">final</span> <span class="keyword">long</span> end_time)</span> </span>&#123;</span><br><span class="line">        timeListener.timeStart();</span><br><span class="line"></span><br><span class="line">        contine_tag = <span class="keyword">true</span>;</span><br><span class="line">        <span class="keyword">this</span>.end_time = end_time;</span><br><span class="line">        <span class="keyword">this</span>.term_no = term_no;</span><br><span class="line">        <span class="keyword">if</span> (mTimerMap.get(term_no) == <span class="keyword">null</span>) &#123;</span><br><span class="line">            Timer timer = <span class="keyword">new</span> Timer();</span><br><span class="line">            Term_view_bean term_view_bean = <span class="keyword">new</span> Term_view_bean(timer, term_no, end_time);</span><br><span class="line">            mTimerMap.put(term_no, term_view_bean);</span><br><span class="line">            mTimerMap.get(term_no).getTimer().schedule(<span class="keyword">new</span> TimerTask() &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                    <span class="keyword">if</span> (contine_tag) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (is_over) &#123;</span><br><span class="line">                            <span class="keyword">this</span>.cancel();</span><br><span class="line">                            <span class="keyword">return</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                        mHandler.sendEmptyMessage(ticket_mode);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;, <span class="number">0</span>, <span class="number">1000</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stop</span><span class="params">(<span class="keyword">int</span> position_term_no)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mTimerMap.get(position_term_no) != <span class="keyword">null</span>) &#123;</span><br><span class="line">            mTimerMap.get(position_term_no).getTimer().cancel();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stopAll</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Iterator it = mTimerMap.entrySet().iterator();</span><br><span class="line">            <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">                Map.Entry entry = (Map.Entry) it.next();</span><br><span class="line">                <span class="keyword">int</span> key = (<span class="keyword">int</span>) entry.getKey();</span><br><span class="line">                Term_view_bean bean = (Term_view_bean) entry.getValue();</span><br><span class="line">                bean.getTimer().cancel();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        contine_tag = <span class="keyword">false</span>;</span><br><span class="line">        is_over = <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stopSaveValue</span><span class="params">(<span class="keyword">int</span> value)</span> </span>&#123;</span><br><span class="line">        Iterator it = mTimerMap.entrySet().iterator();</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">            Map.Entry entry = (Map.Entry) it.next();</span><br><span class="line">            <span class="keyword">int</span> key = (<span class="keyword">int</span>) entry.getKey();</span><br><span class="line">            <span class="keyword">if</span> (key != value) &#123;</span><br><span class="line">                Term_view_bean bean = (Term_view_bean) entry.getValue();</span><br><span class="line">                bean.getTimer().cancel();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 格式化</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> second</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> String <span class="title">second2TimeSecond</span><span class="params">(<span class="keyword">long</span> second)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">long</span> days = second / (<span class="number">3600</span> * <span class="number">24</span>);</span><br><span class="line">        <span class="keyword">long</span> hours = (second / <span class="number">3600</span>) % <span class="number">24</span>;</span><br><span class="line">        <span class="keyword">long</span> minutes = (second % <span class="number">3600</span>) / <span class="number">60</span>;</span><br><span class="line">        <span class="keyword">long</span> seconds = second % <span class="number">60</span>;</span><br><span class="line"></span><br><span class="line">        String showTime = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">if</span> (days &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            showTime = days + <span class="string">&quot;天 &quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (hours &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (hours &lt; <span class="number">10</span>) &#123;</span><br><span class="line">                showTime += <span class="string">&quot;0&quot;</span> + hours + <span class="string">&quot;时 &quot;</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                showTime += hours + <span class="string">&quot;时 &quot;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (minutes &lt; <span class="number">10</span>) &#123;</span><br><span class="line">            showTime += <span class="string">&quot;0&quot;</span> + minutes + <span class="string">&quot;:&quot;</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            showTime += <span class="string">&quot;&quot;</span> + minutes + <span class="string">&quot;:&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (seconds &lt; <span class="number">10</span>) &#123;</span><br><span class="line">            showTime += <span class="string">&quot;0&quot;</span> + seconds;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            showTime += <span class="string">&quot;&quot;</span> + seconds;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> showTime;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取当前奖期</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getTerm_no</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> term_no;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 修正当前倒计时信息</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateTimer</span><span class="params">(<span class="keyword">final</span> <span class="keyword">int</span> term_no, <span class="keyword">final</span> <span class="keyword">long</span> end_time)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.end_time = end_time;</span><br><span class="line">        <span class="keyword">this</span>.term_no = term_no;</span><br><span class="line">        <span class="keyword">if</span> (mTimerMap.get(term_no) == <span class="keyword">null</span>) &#123;</span><br><span class="line">            start(term_no, end_time);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            mTimerMap.get(term_no).setEnd_time(end_time);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TimeListener</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">timedown</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">buyDown</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">timeStart</span><span class="params">()</span></span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Term_view_bean</span> </span>&#123;</span><br><span class="line">        <span class="keyword">private</span> Timer timer;<span class="comment">// 定时器</span></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">int</span> integer;<span class="comment">// 奖期</span></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">long</span> end_time;<span class="comment">// 结束时间</span></span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Term_view_bean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Term_view_bean</span><span class="params">(Timer timer, <span class="keyword">int</span> integer, <span class="keyword">long</span> end_time)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.timer = timer;</span><br><span class="line">            <span class="keyword">this</span>.integer = integer;</span><br><span class="line">            <span class="keyword">this</span>.end_time = end_time;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Timer <span class="title">getTimer</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> timer;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTimer</span><span class="params">(Timer timer)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.timer = timer;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getInteger</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> integer;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setInteger</span><span class="params">(<span class="keyword">int</span> integer)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.integer = integer;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">long</span> <span class="title">getEnd_time</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> end_time;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setEnd_time</span><span class="params">(<span class="keyword">long</span> end_time)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.end_time = end_time;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/%E8%87%AA%E5%AE%9A%E4%B9%89View/" rel="tag"># 自定义View</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/Android%E5%90%84%E4%B8%AA%E7%89%88%E6%9C%AC%E7%89%B9%E6%80%A7%E7%AE%80%E4%BB%8B/" rel="prev" title="Android各个版本特性简介">
      <i class="fa fa-chevron-left"></i> Android各个版本特性简介
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/25/Android%E8%A7%A3%E5%86%B3%E5%8D%A1%E9%A1%BF%EF%BC%8C%E4%BB%8E%E9%81%BF%E5%85%8DOverdraw%E5%BC%80%E5%A7%8B/" rel="next" title="Android解决卡顿，从避免Overdraw开始">
      Android解决卡顿，从避免Overdraw开始 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%9C%80%E6%B1%82"><span class="nav-number">1.</span> <span class="nav-text">需求</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%88%86%E6%9E%90"><span class="nav-number">2.</span> <span class="nav-text">分析</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.</span> <span class="nav-text">实现</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8A%A0%E8%BD%BD%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AD%97%E4%BD%93"><span class="nav-number">3.1.</span> <span class="nav-text">加载自定义字体</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%80%E5%A7%8B%E8%AE%A1%E6%97%B6"><span class="nav-number">3.2.</span> <span class="nav-text">开始计时</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9D%9F"><span class="nav-number">3.3.</span> <span class="nav-text">结束</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%97%B6%E9%97%B4%E6%98%BE%E7%A4%BA%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="nav-number">3.4.</span> <span class="nav-text">时间显示格式化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BF%AE%E6%AD%A3%E5%80%92%E8%AE%A1%E6%97%B6"><span class="nav-number">3.5.</span> <span class="nav-text">修正倒计时</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8"><span class="nav-number">3.6.</span> <span class="nav-text">使用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="nav-number">4.</span> <span class="nav-text">完整代码</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
